import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 禁止路由点击多次，控制台报错 NavigationDuplicated
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => console.error(err))
}

// children
const ms = require.context('./modules', false, /\.js$/)
const modules = []
ms.keys().forEach(k => {
  modules.push(...ms(k).default)
});

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import( /* webpackChunkName: 'homepage' */ '../layout/Layout'),
    meta: {
      title: '首页'
    },
    children: modules
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
